layui.define('layer', function(exports) {
    var $layer = {
        json: {
            type: '',
            title: '',
            content: '',
            area: '',
            //skin:'',
            //offset: ,
            btnAlign: 'c',
            closeBtn: 0,
            shade: [0.5, '#000'],
            shadeClose: false,
            time: 0,
            id: 'onlyOne',
            anim: 0,
            resize: false,
            yes: null,
            btn2: null,
        },
        alertIfOne: function(title, content, area, fn) {
            this.json.type = 2;
            this.json.btn = ['确定'];
            this.json.title = title;
            this.json.content = content;
            this.json.area = area;
            this.json.yes = function(index, layero) {
                fn && fn();
                layer.close(index);
            };

            var json3 = this.json;
            json3.closeBtn = 1;
            layer.open(json3);
        },
        alertHOne: function(title, content, area, fn) {
            this.json.type = 1;
            this.json.btn = ['确定'];
            this.json.title = title;
            this.json.content = content;
            this.json.area = area;
            this.json.yes = function(index, layero) {
                fn && fn();
                layer.close(index);
            };

            var json3 = this.json;
            json3.closeBtn = 1;
            layer.open(json3);
        },
        alertNoH: function(title, content, area, fn) {
            this.json.type = 1;
            this.json.title = title;
            this.json.content = content;
            this.json.area = area;

            var json3 = this.json;
            json3.anim = 4;
            layer.open(json3);
            fn && fn();
        },
        alertNoIf: function(title, content, area, fn) {
            this.json.type = 2;
            this.json.title = title;
            this.json.content = content;
            this.json.area = area;

            var json3 = this.json;
            json3.closeBtn = 1;
            json3.btn = [];

            layer.open(json3);
        },
        alerTip: function(title, fn) {
            layer.msg(title);
            fn && fn();
        }
    };

    exports('$layer', $layer);
});

layui.define('layer', function(exports) {
    var $ = layui.jquery,
        layer = layui.layer;

    var $ajax = {
        ajaxResponse: function(jsonParam) {
            //初始化参数，防止其他参数没有传
            var _url = '../../ahtmls/login.html';
            jsonParam.method = typeof(jsonParam.method) != 'undefined' ? jsonParam.method : 'POST';
            jsonParam.dAsync = typeof(jsonParam.dAsync) != 'undefined' ? jsonParam.dAsync : false;
            jsonParam.dType = typeof(jsonParam.dType) != 'undefined' ? jsonParam.dType : 'json';
            jsonParam.params = typeof(jsonParam.params) != 'undefined' ? jsonParam.params : {};

            jsonParam.linkURL = typeof(jsonParam.linkURL) != 'undefined' ? jsonParam.linkURL : _url;
            jsonParam.selectOther = typeof(jsonParam.selectOther) != 'undefined' ? jsonParam.selectOther : 'body';
            jsonParam.selectTimeout = typeof(jsonParam.selectTimeout) != 'undefined' ? jsonParam.selectTimeout : 'body';
            var _href = 'back_login.html';

            var html = '<div class="sk-spinner sk-spinner-fading-circle">' +
                '<div class="sk-circle1 sk-circle"></div>' +
                '<div class="sk-circle2 sk-circle"></div>' +
                '<div class="sk-circle3 sk-circle"></div>' +
                '<div class="sk-circle4 sk-circle"></div>' +
                '<div class="sk-circle5 sk-circle"></div>' +
                '<div class="sk-circle6 sk-circle"></div>' +
                '<div class="sk-circle7 sk-circle"></div>' +
                '<div class="sk-circle8 sk-circle"></div>' +
                '<div class="sk-circle9 sk-circle"></div>' +
                '<div class="sk-circle10 sk-circle"></div>' +
                '<div class="sk-circle11 sk-circle"></div>' +
                '<div class="sk-circle12 sk-circle"></div>' +
                '</div>';

            $.ajax({
                type: jsonParam.method,
                url: jsonParam.surl,
                dataType: jsonParam.dType,
                async: jsonParam.dAsync,
                data: jsonParam.params,
                timeout: 5000,
                beforeSend: function() {
                    $(jsonParam.selectOther).append(html);
                    jsonParam.befFn && jsonParam.befFn();
                },
                success: function(data, textStatus, jqXHR) {
                    $('body').find('.sk-spinner').remove();
                    if (data.code == 100) { //data.code==100  1  成功执行其他操作
                        jsonParam.sFn && jsonParam.sFn(data);
                    } else if (data.code == 2010) { //data.code==2010 用户失效
                        var html = '<div style="line-height:30px; text-align:center;" id="dottThen">' +
                            '<span>用户已失效请点此：</span><a href="' + _href +
                            '" target="_self" style="color:#009688">去登录</a></div>';
                        layer.open({ //2017.03.21修改
                            type: 1,
                            title: false,
                            closeBtn: 0, //不显示关闭按钮
                            shade: [.3],
                            area: ['200px', '30px'],
                            //offset: 'rb', //右下角弹出
                            //time: 3000, //2秒后自动关闭
                            anim: 4,
                            content: html //['test/guodu.html', 'no'], //iframe的url，no代表不显示滚动条
                        });
                    } else if (data.code == 2001) { //没权限,3s后跳转指定页面
                        layer.msg(data.message);
                    } else {
                        layer.msg(data.message);
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    $('body').find('.sk-spinner').remove();
                    if (textStatus == 'timeout') {
                        //ajaxErr(jsonParam.selectTimeout);
                    } else {
                        if (XMLHttpRequest.status == 404) {
                            //console.log(2);
                        } else if (XMLHttpRequest.status == 502) {

                        } else {

                        }
                    }
                }
            });
        }
    }


    exports('$ajax', $ajax);
});